package com.education.expenses.service;

import com.education.expenses.assembler.UserAssembler;
import com.education.expenses.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 * User: apolozkov
 * Date: 8/8/11
 * Time: 4:32 PM
 */
@Service
public class SecurityService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SaltSource saltSource;

    @Autowired
    private UserAssembler userAssembler;

    public void secureUserPassword(User user) {
        UserDetails userDetails = userAssembler.convertUserToUserDetails(user);
        String password = userDetails.getPassword();
        Object salt = saltSource.getSalt(userDetails);
        String encodedPassword = passwordEncoder.encodePassword(password, salt);
        user.setPassword(encodedPassword);
    }
}
